'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const CircularDependencyPlugin = require('circular-dependency-plugin')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || '全友家居' // page title
// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
const port = 9527 // dev port
// const locationUrl = 'http://192.168.99.30:4002'
// const locationUrl = 'https://192.168.99.109:4002'
// const url = 'http://qup.gateway.itdev.quanyou.com.cn'
const url = 'https://qup2dev.quanyou.com.cn/'
// 话务条
// const callBarURL = 'https://calluat.quanyou.com.cn/api/xing-callproxy'
// const callBarURL = 'http://192.168.79.131/api/xing-callproxy'
const callBarURL = 'https://ccdev.quanyou.com.cn/api/xing-callproxy'
const callBarURL2 = 'https://ccdev.quanyou.com.cn'
// dev api v1
const apiv1 = 'http://call-center-api.itdev2.quanyou.com.cn'
// const apiv1 = 'http://192.168.75.212:8558'
// const apiv1 = 'http://127.0.0.1:9100'

module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'call-center/static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  css: {
    loaderOptions: {
      sass: {
        data: `
          @import '~qup-component/theme/qup-theme.scss';
        `
      }
    }
  },
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // https: true,
    // proxy: {
    //   '/call-center/api': {
    //     target: 'http://192.168.75.212:8558',
    //     pathRewrite: {
    //       '^/call-center/api': '/api'
    //     }
    //   }
    // }
    proxy: 'https://ccdev.quanyou.com.cn'
    // proxy: {
    //   '^/api': {
    //     'target': 'https://ngcc.bytedance.com',
    //     'changeOrigin': true,
    //     'headers': {
    //       'origin': 'https://ccdev.quanyou.com.cn' // origin 设置为接入方系统的域名
    //     }},
    //   '/auth': {
    //     target: url,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/auth': '/auth'
    //     }
    //   },
    //   '/ucenter/api': {
    //     target: url,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/ucenter/api': '/ucenter/api'
    //     }
    //   },
    //   '/select-service/api': {
    //     target: 'http://select-service.itdev2.quanyou.com.cn',
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/select-service/api': '/api'
    //     }
    //   },
    //   '/data-dict/api': {
    //     target: url,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/data-dict/api': '/data-dict/api'
    //     }
    //   },
    //   '/baseService/api': {
    //     target: url,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/baseService/api': '/baseService/api'
    //     }
    //   },
    //   // '/v1': {
    //   //   target: 'http://172.18.158.50:4002',
    //   //   changeOrigin: true,
    //   //   pathRewrite: {
    //   //     '^/v1': '/v1'
    //   //   }
    //   // },
    //   '/code': {
    //     target: url,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/code': '/code'
    //     }
    //   },
    //   '/gen': {
    //     target: url,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/gen': '/gen'
    //     }
    //   },
    //   // 在正式环境获取前缀
    //   // '^/call-center/api/v1/prefixNumberInfo/queryOneAvailable/': {
    //   //   target: 'http://127.0.0.1:9100',
    //   //   changeOrigin: true,
    //   //   pathRewrite: {
    //   //     // '^/call-center/api/v1': '/api/v1'
    //   //   },
    //   //   protocolRewrite: 'http'
    //   // },
    //   '^/call-center/api/v1': {
    //     target: apiv1,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/call-center/api/v1': '/api/v1'
    //     },
    //     protocolRewrite: 'http'
    //   },
    //   '^/call-center/api/v2': {
    //     target: apiv1,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/call-center/api/v2': '/api/v2'
    //     },
    //     protocolRewrite: 'http'
    //   },
    //   '^/call-center/img': {
    //     target: apiv1,
    //     changeOrigin: true,
    //     protocolRewrite: 'http'
    //   },
    //   '^/img': {
    //     target: apiv1,
    //     changeOrigin: true,
    //     protocolRewrite: 'http'
    //   },
    //   '^/call-center/openapi/v1': {
    //     target: apiv1,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/call-center/openapi/v1': '/openapi/v1'
    //     },
    //     protocolRewrite: 'http'
    //   },
    //   '^/call-center/openapi/v2': {
    //     target: apiv1,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/call-center/openapi/v2': '/openapi/v2'
    //     },
    //     protocolRewrite: 'http'
    //   },
    //   // 录音文件
    //   '^/file': {
    //     target: apiv1,
    //     changeOrigin: true,
    //     protocolRewrite: 'http'
    //   },
    //   // 话务条
    //   '^/callService': {
    //     target: callBarURL,
    //     changeOrigin: true,
    //     ws: true
    //     // protocolRewrite: 'https'
    //   }
    // }
    // after: require('./mock/mock-server.js')
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      new CircularDependencyPlugin({
        // exclude detection of files based on a RegExp
        exclude: /node_modules/,
        // include specific files based on a RegExp
        include: /src/,
        // add errors to webpack instead of warnings
        failOnError: true,
        // allow import cycles that include an asyncronous import,
        // e.g. via import(/* webpackMode: "weak" */ './file.js')
        allowAsyncCycles: false,
        // set the current working directory for displaying module paths
        cwd: process.cwd()
      })
    ]
  },
  chainWebpack(config) {
    const cdn = {
      // inject tinymce into index.html
      // why use this cdn, detail see https://github.com/PanJiaChen/tinymce-all-in-one
      js: [
        // 'https://cdn.jsdelivr.net/npm/tinymce-all-in-one@4.9.2/tinymce.min.js'
      ]
    }
    config.plugin('html')
      .tap(args => {
        args[0].cdn = cdn
        args[0].lastBuildDatetime = new Date().toLocaleString()
        return args
      })
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
    // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
